package main

import (
	"fmt"
)

type Record struct {
	detail    string
	money     float64
	exitsFlag bool
	typeMap   map[int]string
}

func NewRecord() *Record {
	detail := "-------------当前收支明细记录--------------"
	detail += "\n收支\t账户金额\t收支金额\t说    明"
	return &Record{
		detail: detail,
		typeMap: map[int]string{
			2: "收入",
			3: "支出",
		},
	}
}
func (r *Record) printDetail() {
	fmt.Println(r.detail)
}
func (r *Record) increMoney(choice int) {
	account := 0.0
	fmt.Print("本次收入金额:")
	fmt.Scanln(&account)

	desc := ""
	fmt.Print("本次收入说明:")
	fmt.Scanln(&desc)

	r.money += account
	r.detail += fmt.Sprintf("\n%v\t%v\t%v\t%v\t", r.typeMap[choice], r.money, account, desc)
	log("登记完成")
}
func (r *Record) decreMoney(choice int) {
	account := 0.0
	fmt.Print("本次支出金额:")
	fmt.Scanln(&account)

	desc := ""
	fmt.Print("本次支出说明:")
	fmt.Scanln(&desc)

	r.money += account
	r.detail += fmt.Sprintf("\n%v\t%v\t%v\t%v\t", r.typeMap[choice], r.money, account, desc)
	log("登记完成")
}
func (r *Record) exitSystem() {
	var input string
	fmt.Print("确认要退出吗,y/n?:")
	fmt.Scanln(&input)
	for {
		if input == "Y" || input == "y" {
			//确认退出 跳出外循环
			r.exitsFlag = true
			break
		} else if input == "n" || input == "N" {
			//不退出 跳出 for 循环 继续走外循环
			break
		} else {
			//输入有误重新输入
			fmt.Print("输入有误,请重新输入:")
			fmt.Scanln(&input)
		}
	}
}
func (r *Record) showMemu() {
	for {
		log("家庭收支记账软件")
		fmt.Println("1.收支明细")
		fmt.Println("2.登记收入")
		fmt.Println("3.登记支出")
		fmt.Println("4.退   出")
		var choice int
		for {
			fmt.Print("请选择(1-4):")
			fmt.Scanln(&choice)
			if choice <= 0 || choice > 4 {
				fmt.Println("输入有误!")
				continue
			}
			break
		}
		switch choice {
		case 1:
			r.printDetail()
		case 2:
			r.increMoney(choice)
		case 3:
			r.decreMoney(choice)
		case 4:
			r.exitSystem()
		}
		if r.exitsFlag {
			break
		}
	}
}

func main() {
	b := NewRecord()
	b.showMemu()
}

func guocheng() {
	detail := "-------------当前收支明细记录--------------"
	detail += "\n收支\t账户金额\t收支金额\t说    明"
	money := 0.0
	exitsFlag := false
	typeMap := map[int]string{
		2: "收入",
		3: "支出",
	}
	for {
		log("家庭收支记账软件")
		fmt.Println("1.收支明细")
		fmt.Println("2.登记收入")
		fmt.Println("3.登记支出")
		fmt.Println("4.退   出")
		var choice int
		for {
			fmt.Print("请选择(1-4):")
			fmt.Scanln(&choice)
			if choice <= 0 || choice > 4 {
				fmt.Println("输入有误!")
				continue
			}
			break
		}
		switch choice {
		case 1:
			fmt.Println(detail)
		case 2:
			account := 0.0
			fmt.Print("本次收入金额:")
			fmt.Scanln(&account)

			desc := ""
			fmt.Print("本次收入说明:")
			fmt.Scanln(&desc)

			money += account
			detail += fmt.Sprintf("\n%v\t%v\t%v\t%v\t", typeMap[choice], money, account, desc)
			log("登记完成")
		case 3:
			account := 0.0
			fmt.Print("本次支出金额:")
			fmt.Scanln(&account)

			desc := ""
			fmt.Print("本次支出说明:")
			fmt.Scanln(&desc)

			money += account
			detail += fmt.Sprintf("\n%v\t%v\t%v\t%v\t", typeMap[choice], money, account, desc)
			log("登记完成")
		case 4:
			var input string
			fmt.Print("确认要退出吗,y/n?:")
			fmt.Scanln(&input)
			for {
				if input == "Y" || input == "y" {
					//确认退出 跳出外循环
					exitsFlag = true
					break
				} else if input == "n" || input == "N" {
					//不退出 跳出 for 循环 继续走外循环
					break
				} else {
					//输入有误重新输入
					fmt.Print("输入有误,请重新输入:")
					fmt.Scanln(&input)
				}
			}
		}
		if exitsFlag {
			break
		}
	}
}

func log(str string) {
	fmt.Printf("------%v------\n", str)
}
